package com.lwl.service.impl.sds;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.entity.*;
import com.lwl.entity.sds.SdsTongji1;
import com.lwl.entity.sds.SdsTongji2;
import com.lwl.entity.sds.TSdsCallback;
import com.lwl.entity.sds.TSdsRecord;
import com.lwl.service.sds.TSdsRecordService;
import com.lwl.dao.sds.TSdsRecordMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TSdsRecord)表服务实现类
 *
 * @author Lei
 * @since 2022-11-10 09:37:50
 */
@Service
public class TSdsRecordServiceImpl implements TSdsRecordService {
   @Autowired
   private TSdsRecordMapper tSdsRecordDao;

   @Override
   public TSdsRecord checkRecord(TSdsRecord tSdsRecord) {
      TSdsRecord tSdsRecord1 = tSdsRecordDao.selectOne(tSdsRecord);
      return tSdsRecord1;
   }

   @Override
   public int insertRecord(TSdsRecord tSdsRecord) {
      tSdsRecord.setCreatetime(new Date());
      int insert = tSdsRecordDao.insert(tSdsRecord);
//找到刚刚添加用户记录的id
      Example example = new Example(TSdsRecord.class);
      example.createCriteria().andEqualTo("tel",tSdsRecord.getTel());
      List<TSdsRecord> tSdsRecords = tSdsRecordDao.selectByExample(example);
      TSdsRecord tSdsRecord1 = tSdsRecords.get(0);
      return tSdsRecord1.getId();
   }

   @Override
   public PageInfo queryAll(MyPage myPage, TSdsRecord tSdsRecord) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TSdsRecord> tMhtRecords = tSdsRecordDao.queryAll(tSdsRecord);
      PageInfo pageInfo = new PageInfo(tMhtRecords);
      return pageInfo;
   }

   @Override
   public PageInfo getRecordDetails(MyPage myPage, Integer id) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());
//      这里是为了接收查询结果，因为查询结果并不在同一个实体类中
      List<Map<String,Object>> details = tSdsRecordDao.getRecordDetails(id);
      PageInfo pageInfo = new PageInfo(details);
      return pageInfo;
   }

   @Override
   public PageInfo getStatistics2(MyPage myPage, SdsTongji2 sdsTongji2) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      PageInfo pageInfo = new PageInfo(tSdsRecordDao.getStatistics2(sdsTongji2));
      return pageInfo;
   }

   @Override
   public List<SdsTongji1> getStatistics1() {
      List<SdsTongji1> sdsTongji1List = tSdsRecordDao.getStatics1();
      return sdsTongji1List;
   }

   @Override
   public boolean checkId(Integer id) {
      List<TSdsCallback> tSdsCallbacks = tSdsRecordDao.selectCallBack(id);
      System.out.println("根据id查询到的回访记录是tMhtCallback = " + tSdsCallbacks);
      if (tSdsCallbacks.size()==0){
         System.out.println("没有查到该用户对应的回访记录");
         return true;
      }else{
         System.out.println("查到了该用户的回访记录");
         return false;
      }
   }

   @Override
   public boolean queryByTestId(Integer id) {
      TSdsRecord tSdsRecord = tSdsRecordDao.queryByTestId(id);
      if (tSdsRecord!=null){
         return false;
      }else{
         return true;
      }
   }
}
